package com.dxl.producer;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * Description: 消息生产者类-发布订阅模式
 *
 * @author: Yingyu Wei
 * <p>File Created at 2021/5/1</p>
 */
@Component
public class FanoutProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${order.fanout}")
    private String exchange;

    public void saveOrder(Long userId, Long goodsId, Integer buyNum) {

        String msg = "用户：" + userId + ",购买了商品：" + goodsId + ",订单编号：" + UUID.randomUUID().toString();
        /**
         * String exchange: 交换机名称
         * String routingKey: 路由key
         * final Object object: 消息内容
         */
        rabbitTemplate.convertAndSend(exchange, "", msg);

    }
}
